package data
{
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.data.LoaderMaxVars;
	
	import flash.display.Bitmap;
	
	import mx.collections.ArrayCollection;

	public class StartPage
	{
		public var title:String;
		public var description:String
		public var backgroundURL:String;
		public var backgroundImage:Bitmap;
		public var mainTopics:ArrayCollection;
		
		public var maintopicAssetsLoader:LoaderMax;
		public var attractionAssetsLoader:LoaderMax;
		
		public function StartPage()
		{
			
		}
		
		//maintopic assets
		public function getMaintopicAssetsLoader():LoaderMax
		{
			var vars:LoaderMaxVars = new LoaderMaxVars();
			vars.auditSize(false);
			vars.maxConnections(2);
			
			maintopicAssetsLoader = new LoaderMax(vars);
			maintopicAssetsLoader.addEventListener(LoaderEvent.COMPLETE,onMaintopicAssetsLoadComplete);
			
			var backgroundImageLoader:ImageLoader = new ImageLoader(this.backgroundURL);
			backgroundImageLoader.addEventListener(LoaderEvent.COMPLETE, onBackgroundLoadComplete);
			maintopicAssetsLoader.append(backgroundImageLoader);
			
			for each(var mainTopic:MainTopic in mainTopics)
			{
				maintopicAssetsLoader.append(mainTopic.getImageLoader());
			}
			
			return maintopicAssetsLoader;
		}
		private function onMaintopicAssetsLoadComplete(e:LoaderEvent):void
		{
			
		}
		private function onBackgroundLoadComplete(e:LoaderEvent):void
		{
			var ldr:ImageLoader = ImageLoader(e.target);
			ldr.removeEventListener(LoaderEvent.COMPLETE,onMaintopicAssetsLoadComplete);
			
			this.backgroundImage = ldr.rawContent;				
			ldr = null;
		}
		
		
		//Attraction Assets
		public function getAttractionAssetsLoader():LoaderMax
		{
			var vars:LoaderMaxVars = new LoaderMaxVars();
			vars.auditSize(false);
			vars.maxConnections(2);
			
			attractionAssetsLoader = new LoaderMax(vars);
			
			for each(var mainTopic:MainTopic in mainTopics)
			{
				var loader:LoaderMax = mainTopic.getAttractionLoader();
				
				attractionAssetsLoader.append(loader);
			}
			
			attractionAssetsLoader.load();
			return attractionAssetsLoader;
		}
		
		
		
		
		
		//===============================
		public function toString():String
		{
			var returnString:String = "";
			
			returnString += "################\n";
			returnString += "Startpage: "+ title + " description: " + description + " backgroundURL: " + backgroundURL + "\n";
			
			for each(var maintopic:MainTopic in mainTopics)
			{
				returnString += maintopic.toString();
			}
			
			return returnString;
		}
	}
}